home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / usr / share / hal / device-manager / LibGladeApplication.py < prev    next >
Encoding:
Python Source  |  2007-05-11  |  1.9 KB  |  66 lines

  1. """This module contains the LibGladeApplication class."""
  2.  
  3. import signal
  4. import gtk
  5. from gtk import glade
  6.  
  7. from GtkAttributesFacade import GtkAttributesFacade 
  8.  
  9.  
  10. class LibGladeApplication:
  11.  
  12.     """This is the base class for applications that use Glade.
  13.     
  14.     The following attributes are used:
  15.  
  16.     xml - This is an instance of glade.XML which encapsulates the Glade GUI.
  17.  
  18.     The following private variables are used:
  19.  
  20.     _signalsAreDone - The UNIX signal handlers only have to be set once. 
  21.  
  22.     """
  23.  
  24.     _signalsAreDone = False
  25.  
  26.     def __init__(self, gladeFile):
  27.         """Setup the appropriate signal handlers and call setHandlers."""
  28.         if not self._signalsAreDone:
  29.             self._signalsAreDone = True
  30.             signal.signal(signal.SIGINT, signal.SIG_DFL)
  31.         self.xml = glade.XML(gladeFile)
  32.         self.setHandlers()
  33.  
  34.     def setHandlers(self):
  35.         """Automatically autoconnect all of the handlers.
  36.         
  37.         Any methods (even in subclasses) that start with "on_" will be treated
  38.         as a handler that is automatically connected to by
  39.         xml.signal_autoconnect.
  40.  
  41.         """
  42.         handlers = {}
  43.         for i in dir(self):
  44.             if i.startswith("on_"):
  45.                 handlers[i] = getattr(self, i)
  46.         self.xml.signal_autoconnect(handlers)
  47.  
  48.     def __getattr__(self, name):
  49.         """If self doesn't have the attribute, check self.xml.
  50.  
  51.         If self.xml does have the attribute, wrap it in a GtkAttributesFacade 
  52.         instance, cache it in self, and return it.
  53.         
  54.         """
  55.         obj = self.xml.get_widget(name)
  56.         if obj:
  57.             obj = GtkAttributesFacade(obj) 
  58.             setattr(self, name, obj)
  59.             return obj
  60.         raise AttributeError("%s instance has no attribute '%s'" % 
  61.             (self.__class__.__name__, name))
  62.  
  63.     def on_quit_activate(self, *args):
  64.         """Ignore args and call gtk.mainquit()."""
  65.         gtk.main_quit()
  66.